var express = require('express');
var router = express.Router();
const passport = require('passport');
var commentDAL = require('../DAL/commentDAL');
//添加主评论
router.post('/addMainComment', passport.authenticate('jwt', { session: false}),function(req, res, next) {
    var com = req.body
    commentDAL.addMainComment(com, function(err) {
        if (err) {
            res.json({
                code: 500,
                msg: '添加主评论失败'
            })
        } else {
            res.json({
                code: 200,
                msg: '添加主评论成功'
            })
        }
    })
});
//添加子评论
router.post('/addSubComment', passport.authenticate('jwt', { session: false}),function(req, res, next) {
    var com = req.body
    commentDAL.addSubComment(com, function(err) {
        if (err) {
            res.json({
                code: 500,
                msg: '添加子评论失败'
            })
        } else {
            res.json({
                code: 200,
                msg: '添加子评论成功'
            })
        }
    })
});
//删除主评论
router.post('/delMainComment', passport.authenticate('jwt', { session: false}),function(req,res,next){
    var com = req.body
    commentDAL.delMainComment(com, function(err) {
        if (err) {
            res.json({
                code: 500,
                msg: '删除主评论失败'
            })
        } else {
            res.json({
                code: 200,
                msg: '删除主评论成功'
            })
        }
    })
});
//删除子评论
router.post('/delSubComment', passport.authenticate('jwt', { session: false}),function(req,res,next){
    var com = req.body
    commentDAL.delSubComment(com, function(err) {
        if (err) {
            res.json({
                code: 500,
                msg: '删除子评论失败'
            })
        } else {
            res.json({
                code: 200,
                msg: '删除子评论成功'
            })
        }
    })
});
//查看主评论
router.get('/getMainComment', passport.authenticate('jwt', { session: false}),function(req,res,next){
    var com = req.query
    commentDAL.getMainComment(com, function(err,results) {
        if (err) {
            res.json({
                code: 500,
                msg: '查看主评论失败'
            })
        } else {
            res.json({
                code: 200,
                msg: '查看主评论成功',
                results: results
            })
        }
    })
});
//查看子评论
router.get('/getSubComment', passport.authenticate('jwt', { session: false}),function(req,res,next){
    var com = req.query
    commentDAL.getSubComment(com, function(err,results) {
        if (err) {
            res.json({
                code: 500,
                msg: '查看子评论失败'
            })
        } else {
            res.json({
                code: 200,
                msg: '查看子评论成功',
                results: results
            })
        }
    })
});
module.exports = router;